<template>
	<div>
		<zh-form ref="noticeFormRef" :span="24" :model="state.form" :rules="state.rules">
			<zh-form-input
				prop="title"
				:disabled="props.showFlag"
				label="标题"
				placeholder="请输入系统状态通知的标题"
				v-model="state.form.title"
			></zh-form-input>
			<zh-form-input
				prop="shortTitle"
				:disabled="props.showFlag"
				label="标题简称"
				placeholder="请输入系统状态通知的标题简称"
				v-model="state.form.shortTitle"
			></zh-form-input>
			<zh-form-select
				:disabled="props.showFlag"
				prop="userTypeList"
				label="查看对象"
				mode="multiple"
				:options="state.dictData.userType"
				placeholder="请选择"
				v-model="state.form.userTypeList"
			></zh-form-select>
			<zh-form-select
				v-if="props.showFlag"
				:disabled="props.showFlag"
				prop="noticeStatus"
				label="状态"
				:options="state.dictData.ARTICLESTATUS"
				placeholder="请选择"
				v-model="state.form.noticeStatus"
			></zh-form-select>
			<zh-form-select
				prop="appId"
				:lazy="true"
				:disabled="props.showFlag"
				label="所属应用"
				v-model="state.form.appId"
				:fieldNames="{ label: 'appName', value: 'appId' }"
				funcCode="application0012"
				searchField="appName"
			></zh-form-select>
			<zh-form-choose-org :disabled="props.showFlag" prop="orgId" label="所属组织" v-model="state.form.orgId"></zh-form-choose-org>
			<zh-form-cascader
				prop="areaCode"
				label="行政区划"
				placeholder="请选择"
				:props="{ checkStrictly: true, emitPath: false }"
				v-model="state.form.areaCode"
				:disabled="props.showFlag"
				:options="state.areaCodeSelectTreeData"
			></zh-form-cascader>
			
			<zh-form-select
				:disabled="props.showFlag"
				prop="linkType"
				label="链接类型"
				:options="state.dictData.linkType"
				v-model="state.form.linkType"
			></zh-form-select>
			<zh-form-input
				prop="linkUrl"
				:disabled="props.showFlag"
				label="链接地址"
				placeholder="请输入链接地址"
				v-model="state.form.linkUrl"
			></zh-form-input>
			<zh-form-input
				prop="linkName"
				:disabled="props.showFlag"
				label="内部路由名"
				placeholder="请输入内部路由名"
				v-model="state.form.linkName"
			></zh-form-input>
			<zh-form-input
				prop="linkPath"
				:disabled="props.showFlag"
				label="内部路由路径"
				placeholder="请输入内部路由路径"
				v-model="state.form.linkPath"
			></zh-form-input>
			<zh-form-input
				prop="linkParams"
				:rows="2"
				type="textarea"
				:disabled="props.showFlag"
				label="链接参数"
				placeholder="请输入链接参数"
				v-model="state.form.linkParams"
			></zh-form-input>
			<zh-form-date
				prop="publishTime"
				label="发布时间"
				v-if="props.showFlag"
				:disabled="props.showFlag"
				placeholder="请输入发布时间"
				type="datetime"
				v-model="state.form.publishTime"
			></zh-form-date>
			<zh-form-date
				:disabled="props.showFlag"
				v-if="props.showFlag"
				prop="offTime"
				label="下架时间"
				placeholder="请输入下架时间"
				type="datetime"
				v-model="state.form.offTime"
			></zh-form-date>
			<zh-form-input
				prop="publisher"
				:disabled="props.showFlag"
				label="发布主体"
				placeholder="请输入发布主体（人或单位）"
				v-model="state.form.publisher"
			></zh-form-input>
			
			<zh-form-input
				:disabled="props.showFlag"
				:span="24"
				:rows="4"
				prop="remarks"
				type="textarea"
				label="备注"
				v-model="state.form.remarks"
			></zh-form-input>
			<zh-form-select
				:disabled="true"
				v-if="props.showFlag"
				prop="authStatus"
				label="审核状态"
				:options="state.dictData.authStatus"
				v-model="state.form.authStatus"
			></zh-form-select>
			<zh-form-date :disabled="true" prop="authTime" label="审核时间" placeholder="" type="datetime" v-model="state.form.authTime"></zh-form-date>
			<zh-form-input
				:disabled="true"
				:span="24"
				v-if="props.showFlag"
				:rows="4"
				prop="authRemarks"
				type="textarea"
				label="审核意见"
				v-model="state.form.authRemarks"
			></zh-form-input>
			<zh-form-choose-user v-if="props.showFlag" :disabled="true" prop="authUserId" label="审核人" v-model="state.form.authUserId"></zh-form-choose-user>
		</zh-form>
	</div>
</template>
<script lang="ts" setup name="NoticeForm">
import { ElMessage } from 'element-plus';
//------------------接收传入参数(组件)-------------------
const props = defineProps({
	noticeInfo: {
		type: Object,
		default: () => {},
		required: false,
	},
	showFlag: {
		type: Boolean,
		default: false,
	},
	modelValue: Boolean,
});
//------------------定义子组件---------------------------
const noticeFormRef = ref();
const emit = defineEmits(['update:modelValue']);
//------------------内部变量定义-------------------------
const state = reactive({
	form: {} as any,
	rules: {
		title: [
			{
				required: true,
				message: '请输入标题',
				trigger: 'blur',
			},
		],
		appId: [
			{
				required: true,
				message: '请选择所属应用',
				trigger: 'blur',
			},
		],
	},

	areaCodeSelectTreeData: [],
	parentSelectTreeData: [],
	codeDisabled: false,
	dictData: {} as any,
});
//------------------定义调用方法-------------------------
function getAreaCodeData() {
	setTimeout(() => {
		$api.common.send.post('dict0026', {}).then((res) => {
			state.areaCodeSelectTreeData = res.result;
		});
	}, 20);
}

async function save() {
	//校验表单
	let checkFlag = await noticeFormRef.value.checkForm();
	if (checkFlag) {
		const info = state.form;
		let ret = null;
		if (info.noticeId) {
			ret = await $api.common.send.post('sysNotice0005', info);
			ret.saveType = 'edit';
			if (ret.code === 0) {
				ElMessage.success('修改成功！');
				emit('update:modelValue', false);
			}
		} else {
			ret = await $api.common.send.post('sysNotice0004', info);
			ret.saveType = 'add';
			if (ret.code === 0) {
				ElMessage.success('新建成功！');
				emit('update:modelValue', false);
			}
		}
		return ret;
	}
	return null;
}
//------------------初始化------------------------------
onMounted(() => {
	getAreaCodeData();
});
if (props.noticeInfo) {
	state.form = Object.assign({}, props.noticeInfo);
}
const params = {
	dictFields: 'ARTICLESTATUS,authStatus,userType',
};
$api.common.getBatchDict.post(params).then((res) => {
	state.dictData = res.result;
});
//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ form: state.form, save });
</script>
<style lang="scss" scoped></style>
